package itsm.isperp.module.controller.wechat;

import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.module.entity.wechat.WechatNews;
import itsm.isperp.module.entity.wechat.WechatNewsArticle;
import itsm.isperp.module.service.wechat.WechatNewsArticleService;
import itsm.isperp.module.service.wechat.WechatNewsService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 控制器
 * 
 * @author lizx
 * @date 2014-11-06 00:07
 */
@Controller
@RequestMapping("wechat/news")
public class WechatNewsController extends BaseController<WechatNews> {

	@Autowired
	protected WechatNewsService wechatNewsService;
	@Autowired
	protected WechatNewsArticleService wechatNewsArticleService;

	@Override
	public WechatNewsService getService() {
		return this.wechatNewsService;
	}

	@Override
	public String getPageTitle() {
		return "图文消息";
	}

	@Override
	@RequestMapping("edit")
	public ModelAndView edit(String id, HttpServletRequest req)
			throws Exception {
		ModelAndView mv = super.edit(id, req);
		List<WechatNewsArticle> articles = new ArrayList<WechatNewsArticle>();
		WechatNewsArticle topArticle = null;
		if (StringUtils.isNotEmpty(id)) {
			articles = this.wechatNewsArticleService.findByNewsId(id,null);

			if (articles.size() > 0) {
				topArticle = articles.get(0);
				articles.remove(0);
			}

		}

		mv.addObject("topArticle", topArticle);
		mv.addObject("articles", articles);
		return mv;
	}
}
